#include "Camera.h"

Camera::Camera(Vector3 p, Vector3 d)
{
	position = p;
	direction = d;

	direction.Normalize();

	calculateMatrices();
}

void Camera::calculateMatrices()
{
	esMatrixLoadIdentity(&view);
	esMatrixLoadIdentity(&proj);

	/*ESMatrix translation, rotation;
	esMatrixLoadIdentity(&translation);
	esMatrixLoadIdentity(&rotation);
	esTranslate(&translation, -position.x, -position.y, -position.z);

	Vector3 origDir (0, 0, 1);
	Vector3 axis = Vector3::Cross(origDir, direction);
	float angle =  acos(Vector3::Dot(origDir, direction)) * (360.0f/3.1415f);
	esRotate(&rotation, angle, axis.x, axis.y, axis.z);*/
	esLookAt(&view, 
		position.x, 
		position.y, 
		position.z, 
		position.x + direction.x, 
		position.y + direction.y, 
		position.z + direction.z);

	//esMatrixMultiply(&view, &translation, &rotation);
	esPerspective(&proj, 45.0f, (float)screenWidth/(float)screenHeight, 0.1f, 50.0f);
}

void Camera::move(Vector3 amount)
{
	position = position + amount;
	calculateMatrices();
}
void Camera::moveTo(Vector3 pos)
{
	position = pos;
	calculateMatrices();
}
void Camera::lookDir(Vector3 dir)
{
	direction = dir;
	direction.Normalize();
	calculateMatrices();
}
void Camera::lookAt(Vector3 lock)
{
	direction = (lock-position);
	direction.Normalize();
	calculateMatrices();
}
void Camera::setViewProj(ShaderInfo shader)
{
	view.Load(shader.view);
    proj.Load(shader.proj);
}